package com.elinshaw.element


abstract class Element {

  def contents: Array[String]

  /**
   * height/width可以实现为字段效率更高
   *
   * @return
   */
  def height: Int = contents.length

  //val height: Int = contents.length

  def width: Int = if (height == 0) 0 else contents(0).length

  //val width: Int = if (height == 0) 0 else contents(0).length

  def demo() = println("Element implementation invoke")

  /**
   * 工厂方法
   *
   * @param contents
   * @return
   */
  def elem(contents: Array[String]): Element = new ArrayElement(contents)

  def elem(ch: Char, height: Int, width: Int): UniFormElement = new UniFormElement(ch, height, width)

  def elem(s: String): Element = new LineElement(s)

  override def toString: String = contents mkString "\n"
}
